package panda.client.ui.widgets;

import java.util.List;

import panda.client.Cardpanda;
import panda.client.JSNI;
import panda.client.engine.MagicCard;
import panda.client.enumerations.CardScale;
import panda.client.enumerations.Color;
import panda.client.enumerations.MagicSet;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;

/** A bar of buttons to click on for actions :P */
public class ActionBar extends Composite {
	private HorizontalPanel shell;
	private Button toggleTokenMenu;
	private Button randomNumberGen;
	private Button redCounterButton;
	private Button greenCounterButton;
	private Button blueCounterButton;
	private Button drawOpeningHand;
	private Button mulligan;
	private Button reset;
	private Button resetAndSwitch;

	public ActionBar() {
		shell = new HorizontalPanel();

		/* toggle the visibility of the token menu */
		toggleTokenMenu = new Button("Add Token");
		setButtonStyle(toggleTokenMenu);
		toggleTokenMenu.setTitle("Toggle the token creation dialog.");
		toggleTokenMenu.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				TokenSummonDialog tokenMenu = Cardpanda.tokenMenu;
				
				if(tokenMenu.isOpen()) {
					tokenMenu.hide();
					Cardpanda.setFocus();
				} else {
					tokenMenu.show();
				}
			}
		});

		/* roll a 20 sided die and display the results in NoteTip */
		randomNumberGen = new Button("D20 Roll");
		setButtonStyle(randomNumberGen);
		randomNumberGen.setTitle("Roll a 20 sided die.");
		randomNumberGen.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				int random = 1 + (int) Math.round(Math.random() * 19);

				String logForMe = "You roll a " + random + ".";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " rolls a " + random + ".";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
				
				Cardpanda.setFocus();
			}
		});

		/* red counter */
		redCounterButton = new Button("Counter");
		initAddCounterButton(redCounterButton, Color.RED);

		/* green counter */
		greenCounterButton = new Button("Counter");
		initAddCounterButton(greenCounterButton, Color.GREEN);

		/* blue counter */
		blueCounterButton = new Button("Counter");
		initAddCounterButton(blueCounterButton, Color.BLUE);

		/* draw an opening hand of 7 cards */
		drawOpeningHand = new Button("Draw 7");
		setButtonStyle(drawOpeningHand);
		drawOpeningHand.setTitle("Start the game by drawing 7 cards for your opening hand.");
		drawOpeningHand.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				Cardpanda.clientModel.drawSevenCards();

				String logForMe = "You draw 7 cards.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " draws 7 cards.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
				
				Cardpanda.setFocus();
			}
		});
		
		/* reset the game state and load both decks from scratch */
		/* TODO this only resets to what the game state was when the client first joins. so might not work correctly for P2 */
		reset = new Button("Reset");
		setButtonStyle(reset);
		reset.setTitle("Reset the game state.");
		reset.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				long currentModelNumber = Cardpanda.clientModel.getClientModelNum();
				Cardpanda.clientModel.syncWithServerModel(Cardpanda.initialBlankModel);
				
				/* Cardpanda.sendModelUpdateToServer will increment this model number appropriately, but it needs to match the current number */
				/* The initialBlankModel has a model number of 0, I think */
				Cardpanda.clientModel.setModelNumber(currentModelNumber);
								
				String logForMe = "You have reset the game state.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " has reset the game state.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
				
				Cardpanda.setFocus();
			}
		});
		
		/* reset the game state and switch decks with the opponent */
		resetAndSwitch = new Button("Switch");
		setButtonStyle(resetAndSwitch);
		resetAndSwitch.setTitle("Reset the game state and switch decks with the opponent.");
		resetAndSwitch.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				long currentModelNumber = Cardpanda.clientModel.getClientModelNum();
				
				/* set the initial blank model to one with the decks swapped; future resets and swaps work properly this way */
				List<MagicCard> tmpP1Library = Cardpanda.initialBlankModel.getP1lib();
				List<MagicCard> tmpP2Library = Cardpanda.initialBlankModel.getP2lib();
				Cardpanda.initialBlankModel.setP1lib(tmpP2Library);
				Cardpanda.initialBlankModel.setP2lib(tmpP1Library);
				
				/* TODO: change ownership */
				
				Cardpanda.clientModel.syncWithServerModel(Cardpanda.initialBlankModel);
				
				/* Cardpanda.sendModelUpdateToServer will increment this model number appropriately, but it needs to match the current number */
				/* The initialBlankModel has a model number of 0, I think */
				Cardpanda.clientModel.setModelNumber(currentModelNumber);
								
				String logForMe = "You have reset the game state and switched decks with your opponent.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " has reset the game state and swapped decklists.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
				
				Cardpanda.setFocus();
			}
		});
		
		/* mulligan */
		mulligan = new Button("Mulligan");
		setButtonStyle(mulligan);
		mulligan.setTitle("Draw a new hand, except you draw one less card.");
		mulligan.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				int currentHandSize = Cardpanda.clientModel.Mulligan();

				String logForMe = "You mulligan to " + currentHandSize + " cards.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " mulligans to " + currentHandSize
						+ " cards.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
				
				Cardpanda.setFocus();
			}
		});
		
		/* convenience button: add Eldrazi Spawn */
		Button spawn = new Button("Eldrazi Spawn");
		setButtonStyle(spawn);
		spawn.setTitle("Put an Eldrazi Spawn token onto the battlefield");
		spawn.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				Cardpanda.grabTokenByExactIDFromServer("Eldrazi Spawn", MagicSet.RISE_OF_THE_ELDRAZI_TOKENS, 1);
				Cardpanda.setFocus();
			}
		});
		

		
		shell.add(reset);
		// DOM.setStyleAttribute(reset.getElement(), "marginRight", "20px");
		DOM.setStyleAttribute(reset.getElement(), "backgroundColor", "rgb(190, 76, 94)");
		DOM.setStyleAttribute(reset.getElement(), "color", "white");
		
		shell.add(randomNumberGen);
		shell.add(toggleTokenMenu);
		shell.add(spawn);
		shell.add(redCounterButton);
		// shell.add(greenCounterButton);
		// shell.add(blueCounterButton);
		/* switch is disabled TODO when switching exchange ownership of all cards */
		// shell.add(resetAndSwitch);
		shell.add(drawOpeningHand);
		shell.add(mulligan);
		shell.setSpacing(3);
		initWidget(shell);
	}

	private void initAddCounterButton(final Button button, final Color color) {
		// DOM.setStyleAttribute(button.getElement(), "backgroundColor", color.toString());
		// DOM.setStyleAttribute(button.getElement(), "color", "white");
		setButtonStyle(button);
		button.setTitle("Add a " + color.getName() + " counter to the playing area");
		button.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				Cardpanda.clientModel.createCounter(color, 40 + CardScale.QUARTER.getWidth() / 2, Cardpanda.canvasHeight
						/ 2 - Counter.height / 2);

				Cardpanda.setFocus();
			}
		});
	}

	private void setButtonStyle(Button b) {
		b.addStyleName("actionBar-RoundedButton");
		
		Element el = b.getElement();
		DOM.setStyleAttribute(el, "border", "1px solid black");

		if(JSNI.getBrowserString().contains("msie 6.0") || JSNI.getBrowserString().contains("msie 7.0")) {
			/* don't use the transparent png. 7.0 supports it but it causes 1px padding in buttons =( */
		} else {
			DOM.setStyleAttribute(el, "backgroundImage", "url(" + NoteTip.GradientURL + ")");
			DOM.setStyleAttribute(el, "backgroundPosition", "bottom");
		}
	}

	// public void setStatusText(String txt) {
	// status.setText(txt);
	// }
}